package com.google.code.training_java_basic.oop;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Random;

public class AlarmGenerator {

	private final String AlarmText[] = { "AIS RECEIVED",
			"FAULT RATE MONITORING", "MAINS BREAKDOWN WITH BATTERY BACK-UP",
			"BCF OPERATION DEGRADED", "TRX OPERATION DEGRADED", "BCF FAULTY",
			"TRX FAULTY", "BCCH MISSING" };
	private final int AlarmNumber[] = { 2909, 2915, 7995, 7601, 7607, 7600,	7606, 7767 };
	private final int randomSeverity[] = { 1, 2, 3 };
	private final String randomAlarmAdditionalInfo[] = { "ET-", "ERDSUP-","1A001-" };
	private Random randomGenerator;

	public AlarmGenerator() {
		
	}

	public static int randBetween(int start, int end) {
		return start + (int) Math.round(Math.random() * (end - start));
	}

	public static Date getRandomDate() {

		GregorianCalendar calendar = new GregorianCalendar();
		int year = 2013;
		calendar.set(Calendar.YEAR, year);
		int dayOfYear = randBetween(1,calendar.getActualMaximum(Calendar.DAY_OF_YEAR));
		int hourOfDay = randBetween(1,calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
		int minuteOfHour = randBetween(0,calendar.getActualMaximum(Calendar.MINUTE));
		int secondOfMinute = randBetween(0,calendar.getActualMaximum(Calendar.SECOND));

		calendar.set(Calendar.DAY_OF_YEAR, dayOfYear);
		calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
		calendar.set(Calendar.MINUTE, minuteOfHour);
		calendar.set(Calendar.SECOND, secondOfMinute);

		return calendar.getTime();

	}

	public Alarm generateAlam() {
 
		int indexRandomAlarmName;
		int indexRandomSeverity;
		int indexRandomAlarmAdditionalInfo;
		int randomAlarmId;

		Alarm alarm = new Alarm();
		randomGenerator = new Random();
		
		randomAlarmId = randomGenerator.nextInt(30000);
		alarm.setAlarmId(randomAlarmId);
		
		indexRandomAlarmName = randomGenerator.nextInt(AlarmText.length);
		alarm.setAlarmText(AlarmText[indexRandomAlarmName]);
		alarm.setAlarmNumber(AlarmNumber[indexRandomAlarmName]);

		indexRandomSeverity = randomGenerator.nextInt(randomSeverity.length);
		alarm.setSeverity(randomSeverity[indexRandomSeverity]);

		indexRandomAlarmAdditionalInfo = randomGenerator.nextInt(randomAlarmAdditionalInfo.length);
		alarm.setAdditionalInfo(randomAlarmAdditionalInfo[indexRandomAlarmAdditionalInfo]);

		Date alarmTime = getRandomDate();
		Date clearTime = getRandomDate();

		/*  50/50 create or not create Active alarm(no clearTime in this case) */
		if(randomGenerator.nextInt(2) == 1) {
			if (clearTime.after(alarmTime)) {
				alarm.setAlarmTime(alarmTime);
				alarm.setClearTime(clearTime);
			} else {
				alarm.setAlarmTime(clearTime);
				alarm.setClearTime(alarmTime);
			}
		} else {

			alarm.setAlarmTime(alarmTime);
		}
		
		return alarm;
	}

	public List<Alarm> generateAlams(int alarmCount) {
		
		int alarmIterator = 0;
		List<Alarm> result = new ArrayList<Alarm>();

		for (alarmIterator = 0; alarmIterator < alarmCount; alarmIterator++) {
				result.add(generateAlam());
		}	
		
		return result;
		
	}

}
